//
//  WorkTaskViewController.m
//  lsOA
//
//  Created by 蓝水软件 on 16/7/22.
//  Copyright © 2016年 苏州蓝水有限公司. All rights reserved.
//

#import "WorkTaskViewController.h"
#import "SubTaskTableViewCell.h"
#import "SubTaskDetailViewController.h"

@interface WorkTaskViewController ()<UIWebViewDelegate,UITableViewDataSource,UITableViewDelegate>{
    
    NSDictionary *data; //主任务明细
    NSArray *dataSubTask;
    UIView *under;
    UIView *under2;
    UIView *under3;
    UILabel *label;
    UITableView *tableSubTask;
    LeftTitleTextField *state;  //状态
    LeftTitleTextField *creater;
    LeftTitleTextField *ReleaseTime;
    LeftTitleTextField *upTime;
    UILabel *manager;
    UILabel *managerLabel;   //管理人员
    LeftTitleTextField *ExpectTime;
    UIWebView *WebViewNote;    //任务内容
    UIWebView *webViewRecords;  //任务操作日志
    
    UIButton *startButton;    //重新启动
    UIButton *stopButton;     //停止
    UIButton *compleButton;   //完成
    UIButton *showContentButton;    //展示内容
}

@end

@implementation WorkTaskViewController

- (void)viewWillAppear:(BOOL)animated{
    [self getData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setUI];
    
    
}

- (void)setValue{
    CGFloat height  = 30*FitSize;
    switch ([data[@"Status"] intValue]) {
        case -1:{
            state.text = @"已停止";
            [startButton mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(height);
            }];
            [stopButton mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(0);
            }];
            [compleButton mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(0);
            }];
            [startButton setTitle:@"重新启动" forState:UIControlStateNormal];
            [stopButton setTitle:@"" forState:UIControlStateNormal];
            [compleButton setTitle:@"" forState:UIControlStateNormal];

        }
            break;
        case 0:{
            state.text = @"进行中";
            [startButton mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(0);
            }];
            [stopButton mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(height);
            }];
            [compleButton mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(height);
            }];
            [startButton setTitle:@"" forState:UIControlStateNormal];
            [stopButton setTitle:@"停止" forState:UIControlStateNormal];
            [compleButton setTitle:@"完成" forState:UIControlStateNormal];
        }
            break;
        case 1:{
            state.text = @"已完成";
            [startButton mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(0);
            }];
            [stopButton mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(0);
            }];
            [compleButton mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(0);
            }];
            [startButton setTitle:@"" forState:UIControlStateNormal];
            [stopButton setTitle:@"" forState:UIControlStateNormal];
            [compleButton setTitle:@"" forState:UIControlStateNormal];
            
        }
            break;
        default:{
            state.text = @"未知状态";
        }
            break;
    }
    label.text = data[@"TaskName"];
    self.title = data[@"TaskName"];
    creater.text = data[@"CreatorRealName"];    //创建人名
    ReleaseTime.text = data[@"AddTime"];    //发布日期
    upTime.text = data[@"UpdateTime"];  //更新日期
    ExpectTime.text = data[@"ExpectTime"];        //期待日期
//    manager.text = @"";
    managerLabel.text = data[@"ManageNameList"]; //管理人
    [managerLabel sizeToFit];
    
    [tableSubTask reloadData];
    [tableSubTask mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(tableSubTask.contentSize.height);
    }];
    [under3 mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(tableSubTask.mas_bottom).offset(10);
    }];
    

    [webViewRecords loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:data[@"Records"]]]];
    
    
}
//获取主任务明细
- (void)getData{
    
    NSString *ashx = @"/Server/TaskHandler.ashx";
    NSDictionary *dic = @{@"act":@"getd",@"fid":self.dic[@"ID"],@"uname":[UserInfo getUserName],@"upwd":[UserInfo getPassWord]};
    [httpPostRequest getDataByPostAshx:ashx and:dic withFinishBlock:^(id model, NSError *error) {
        if (error == nil) {
            NSString *code = [NSString stringWithFormat:@"%@",[model objectForKey:@"code"]];
            if ([code isEqualToString:@"0"]) {
                NSLog(@"任务详情:%@",model);
                data = model[@"data"][0];
                [self getSubtasks];
            }else{
                [XHToast showCenterWithText:[model objectForKey:@"msg"]];
            }
        }else{
            NSLog(@"错误%@",error);
            [XHToast showBottomWithText:@"请求失败"];
        }
    }];
}
//获取子任务列表
- (void)getSubtasks{
    NSString *ashx = @"/Server/TaskHandler.ashx";
    NSDictionary *dic = @{@"act":@"getcl",@"mid":data[@"ID"],@"uname":[UserInfo getUserName],@"upwd":[UserInfo getPassWord]};
    [httpPostRequest getDataByPostAshx:ashx and:dic withFinishBlock:^(id model, NSError *error) {
        if (error == nil) {
            NSString *code = [NSString stringWithFormat:@"%@",[model objectForKey:@"code"]];
            if ([code isEqualToString:@"0"]) {
                NSLog(@"子任务列表:%@",model);
                dataSubTask = model[@"data"];
                [self setValue];
            }else{
                [XHToast showCenterWithText:[model objectForKey:@"msg"]];
            }
        }else{
            NSLog(@"错误%@",error);
            [XHToast showBottomWithText:@"请求失败"];
        }
    }];
    
    
}

- (void)setUI{
    
    WS(ws);
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    [self.view addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(ws.view).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    scrollView.bounces = YES;
    scrollView.scrollEnabled = YES;
    scrollView.scrollsToTop = NO;
    
    UIView *container = [UIView new];
    [scrollView addSubview:container];
    container.backgroundColor = [UIColor colorWithWhite:0.911 alpha:1.000];;
    [container mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(scrollView);
        make.edges.equalTo(scrollView).with.insets(UIEdgeInsetsMake(0,0,0,0));
    }];
    CGFloat height  = 30*FitSize;
    CGFloat width = 250*FitSize;
    
    label = [[UILabel alloc]init];
    label.textAlignment = NSTextAlignmentCenter;
    [container addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(height);
    }];
    
    under = [[UIView alloc]init];
    under.backgroundColor = [UIColor whiteColor];
    [container addSubview:under];
    [under mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        
        make.top.equalTo(label.mas_bottom).offset(5);
    }];
    under.layer.cornerRadius = 10;
    
    UIColor *fontColor = [UIColor colorWithWhite:0.077 alpha:1.000];
    
    
    
    state = [[LeftTitleTextField alloc]initWithUnderLineColor:fontColor height:height width:width title:@"任务状态:"];
    [state hideUnderLine];
    [under addSubview:state];
    [state mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.mas_equalTo(25);
        make.height.mas_equalTo(height);
    }];
    
    creater = [[LeftTitleTextField alloc]initWithUnderLineColor:fontColor height:height width:width title:@"创建人员:"];
    [creater hideUnderLine];
    [under addSubview:creater];
    [creater mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(state.mas_bottom).offset(5);
        make.left.equalTo(state);
        make.height.mas_equalTo(height);
    }];
    
    ReleaseTime = [[LeftTitleTextField alloc]initWithUnderLineColor:fontColor height:height width:width title:@"发布日期:"];
    [under addSubview:ReleaseTime];
    [ReleaseTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(creater.mas_bottom).offset(5);
        make.left.equalTo(state);
        make.height.mas_equalTo(height);
    }];
    [ReleaseTime hideUnderLine];
    
    upTime = [[LeftTitleTextField alloc]initWithUnderLineColor:fontColor height:height width:width title:@"更新日期:"];
    [under addSubview:upTime];
    [upTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ReleaseTime.mas_bottom).offset(5);
        make.left.equalTo(state);
        make.height.mas_equalTo(height);
    }];
    [upTime hideUnderLine];
    
    ExpectTime = [[LeftTitleTextField alloc]initWithUnderLineColor:fontColor height:height width:width title:@"期待日期:"];
    [under addSubview:ExpectTime];
    [ExpectTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(upTime.mas_bottom).offset(5);
        make.left.equalTo(state);
        make.height.mas_equalTo(height);
    }];
    [ExpectTime hideUnderLine];
    
    manager = [[UILabel alloc]init];
    manager.text = @"管理人员";
    manager.textColor = [UIColor blueColor];
    [under addSubview:manager];
    manager.textAlignment = NSTextAlignmentCenter;
    [manager mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ExpectTime.mas_bottom).offset(5);
        make.centerX.equalTo(self.view);
        make.height.mas_equalTo(height);
    }];
    
    UIView *line = [[UIView alloc]init];
    line.backgroundColor = [UIColor lightGrayColor];
    [under addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(manager.mas_bottom).offset(5);
        make.height.mas_equalTo(1);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        
    }];
    
    managerLabel = [[UILabel alloc]init];
    managerLabel.numberOfLines = 0;
    [managerLabel sizeToFit];
    [under addSubview:managerLabel];
    [managerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line.mas_bottom).offset(5);
        make.left.equalTo(state);
        make.right.mas_equalTo(-15);
    }];
    
    
    [under mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(managerLabel.mas_bottom).offset(30);
    }];
    
    UILabel *labelcontent = [[UILabel alloc]init];
    labelcontent.text = @"任务内容";
    labelcontent.textColor = [UIColor colorWithRed:1.000 green:0.168 blue:0.194 alpha:1.000];
    labelcontent.font = FONT(19);
    [container addSubview:labelcontent];
    [labelcontent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(under.mas_bottom).offset(20);
        make.left.equalTo(under);
        make.height.mas_equalTo(height);
    }];
    
    under2 = [[UIView alloc]init];
    [container addSubview:under2];
    [under2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(labelcontent.mas_bottom).offset(5);
        make.left.equalTo(under);
        make.right.equalTo(under);
        make.height.mas_equalTo(50);
    }];
    under2.layer.cornerRadius = 10;
    under2.backgroundColor = [UIColor whiteColor];
    
    WebViewNote = [[UIWebView alloc]init];
    [under2 addSubview:WebViewNote];
    WebViewNote.delegate = self;
    WebViewNote.scalesPageToFit = YES;
    [WebViewNote mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.mas_equalTo(1);
        make.right.mas_equalTo(-1);
        make.bottom.mas_equalTo(-10);
    }];
    WebViewNote.scrollView.bounces = NO;
    
    showContentButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [under2 addSubview:showContentButton];
    [showContentButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(under2);
        make.size.mas_equalTo(CGSizeMake(100, 30));
    }];
    [showContentButton addTarget:self action:@selector(showContent) forControlEvents:UIControlEventTouchUpInside];
    [showContentButton setTitle:@"查看全文" forState:UIControlStateNormal];
    [showContentButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal ];
    
    UILabel *labelRecords = [[UILabel alloc]init];
    labelRecords.text = @"任务操作日志";
    labelRecords.textColor = [UIColor colorWithRed:1.000 green:0.168 blue:0.194 alpha:1.000];
    labelRecords.font = FONT(19);
    [container addSubview:labelRecords];
    [labelRecords mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(under2.mas_bottom).offset(20);
        make.left.equalTo(under);
        make.height.mas_equalTo(height);
    }];
    
    
    UIView *under21= [[UIView alloc]init];
    [container addSubview:under21];
    [under21 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(labelRecords.mas_bottom).offset(5);
        make.left.equalTo(under);
        make.right.equalTo(under);
        make.height.mas_equalTo(300);
    }];
    under21.layer.cornerRadius = 10;
    under21.backgroundColor = [UIColor whiteColor];
    
    webViewRecords = [[UIWebView alloc]init];
    [under21 addSubview:webViewRecords];
    webViewRecords.delegate = self;
    webViewRecords.scalesPageToFit = YES;
    [webViewRecords mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.mas_equalTo(1);
        make.right.mas_equalTo(-1);
        make.bottom.mas_equalTo(-10);
    }];
    webViewRecords.scrollView.bounces = NO;
    
    //子任务表格
    UILabel *labelSubTask = [[UILabel alloc]init];
    labelSubTask.text = @"子任务";
    labelSubTask.textColor = [UIColor colorWithRed:1.000 green:0.168 blue:0.194 alpha:1.000];
    labelSubTask.font = FONT(19);
    [container addSubview:labelSubTask];
    [labelSubTask mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(under21.mas_bottom).offset(20);
        make.left.equalTo(under);
        make.height.mas_equalTo(height);
    }];
    
    under3 = [[UIView alloc]init];
    [container addSubview:under3];
    [under3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(labelSubTask.mas_bottom).offset(20);
        make.left.equalTo(under);
        make.right.equalTo(under);
    }];
    under3.backgroundColor = [UIColor whiteColor];
    under3.layer.cornerRadius = 10;
    
    tableSubTask = [[UITableView alloc]init];
    tableSubTask.delegate = self;
    tableSubTask.dataSource = self;
    tableSubTask.separatorStyle = NO;   //去处cell之间的线条（分割器风格设为NO）
    //    tableSubTask.frame = CGRectMake(0, 10, under3.bounds.size.width, 0);
    [under3 addSubview:tableSubTask];
    [tableSubTask mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(5);
        make.left.mas_equalTo(5);
        make.right.mas_equalTo(-5);
    }];
    
    startButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [startButton setTitle:@"重新启动" forState:UIControlStateNormal];
    startButton.backgroundColor = [UIColor colorWithRed:0.000 green:0.502 blue:0.000 alpha:1.000];
    [container addSubview:startButton];
    startButton.tag = 9000 + 0;
    [startButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(under3.mas_bottom).offset(20);
        make.height.mas_equalTo(height);
        make.left.mas_equalTo(25);
        make.right.mas_equalTo(-15);
    }];
    [startButton addTarget:self action:@selector(TaskOperation:) forControlEvents:UIControlEventTouchUpInside];
    
    stopButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [stopButton setTitle:@"停止" forState:UIControlStateNormal];
    stopButton.backgroundColor = [UIColor redColor];
    [container addSubview:stopButton];
    stopButton.tag = 9000 + 1;
    [stopButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(startButton.mas_bottom).offset(10);
        make.height.mas_equalTo(height);
        make.left.mas_equalTo(25);
        make.right.mas_equalTo(-15);
    }];
    [stopButton addTarget:self action:@selector(TaskOperation:) forControlEvents:UIControlEventTouchUpInside];
    
    compleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [compleButton setTitle:@"完成" forState:UIControlStateNormal];
    compleButton.backgroundColor = [UIColor colorWithRed:0.000 green:0.502 blue:1.000 alpha:1.000];
    [container addSubview:compleButton];
    compleButton.tag = 9000 + 2;
    [compleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(stopButton.mas_bottom).offset(10);
        make.height.mas_equalTo(height);
        make.left.mas_equalTo(25);
        make.right.mas_equalTo(-15);
    }];
    [compleButton addTarget:self action:@selector(TaskOperation:) forControlEvents:UIControlEventTouchUpInside];
    
    [container mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(compleButton.mas_bottom).offset(30);
    }];
}

- (void)showContent{
    showContentButton.hidden = YES;
    [WebViewNote loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:data[@"Notes"]]]];

}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60*FitSize;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return dataSubTask.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellID = @"CellID";
    SubTaskTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
    if (!cell) {
        cell = [[SubTaskTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
    }
    [cell configWithDic:dataSubTask[indexPath.row]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SubTaskDetailViewController *subTaskViewController = [[SubTaskDetailViewController alloc]init];
    subTaskViewController.cid = dataSubTask[indexPath.row][@"ID"];
    NSArray *stringArray = [dataSubTask[indexPath.row][@"ulist"] componentsSeparatedByString:@"#"];
    subTaskViewController.uid = stringArray[0];
    subTaskViewController.mainData = data;
    [self.navigationController pushViewController:subTaskViewController animated:YES];
    
}

//操作任务 停止 完成 重启
- (void)TaskOperation:(UIButton *)sender{
    NSString *ashx = @"/Server/TaskHandler.ashx";
    NSString *apstatus;
    NSString *apstatus2;
    switch (sender.tag - 9000) {
        case 0:{
            apstatus = @"mrestart";
            apstatus2 = @"重启成功";
        }
            break;
        case 1:{
            apstatus = @"mstop";
            apstatus2 = @"停止成功";
        }
            break;
        case 2:{
            apstatus = @"mfinish";
            apstatus2 = @"完成任务";
        }
            break;
    }
    
    NSDictionary *dic = @{@"act":@"apmtask",@"mid":self.dic[@"ID"],@"uname":[UserInfo getUserName],@"upwd":[UserInfo getPassWord],@"apstatus":apstatus};
    [httpPostRequest getDataByPostAshx:ashx and:dic withFinishBlock:^(id model, NSError *error) {
        if (error == nil) {
            NSLog(@"操作任务参数：%@",dic);
            NSString *code = [NSString stringWithFormat:@"%@",[model objectForKey:@"code"]];
            if ([code isEqualToString:@"0"]) {
                NSLog(@"任务操作反回数据:%@",model);
                [XHToast showCenterWithText:apstatus2];
                [startButton setTitle:@"" forState:UIControlStateNormal];
                [stopButton setTitle:@"" forState:UIControlStateNormal];
                [compleButton setTitle:@"" forState:UIControlStateNormal];
                [self getData];
            }else{
                [XHToast showCenterWithText:[model objectForKey:@"msg"]];
            }
        }else{
            NSLog(@"错误%@",error);
            [XHToast showBottomWithText:@"请求失败"];
        }
    }];
}




/**
 *  webView结束加载
 */
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [LBProgressHUD hideAllHUDsForView:webView animated:YES];
    CGSize actualSize = [webView sizeThatFits:CGSizeZero];
    CGRect newFrame = webView.frame;
    newFrame.size.height = actualSize.height;
    webView.frame = newFrame;
    [under2 mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(webView.frame.size.height+30);
    }];
    
    NSString *str = @"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '300%'";
    [webView stringByEvaluatingJavaScriptFromString:str];
}
@end
